[Java] BigDecimal implementation of addition subtraction multiplication and division operation code

  • 2020-06-07 04:34:50
  • OfStack

java.math.BigDecimal

There are four ways to make BigDecimal1. Let me look at two of them first:

Type 1: BigDecimal(double val)

Translates a double into a BigDecimal.

Type 2: BigDecimal(String val)

Translates the String repre sentation of a BigDecimal into a BigDecimal.

Using BigDecimal to construct with String, one addition operation requires converting two floating-point Numbers to String, then creating BigDecimal, calling the add method on one, passing in the other as an argument, and then converting the result of the operation (BigDecimal) to a floating-point number.


public static double add(double v1,double v2)
public static double sub(double v1,double v2)
public static double mul(double v1,double v2)
public static double div(double v1,double v2)
public static double div(double v1,double v2,int scale)
public static double round(double v,int scale)

Tool class: Arith


/**
 *  Due to the Java This utility class provides refinement of the simple types that do not perform precision operations on floating point Numbers   True floating-point arithmetic, including addition, subtraction, multiplication, and division 4 Give up 5 Into the. 
 */
public class Arith { //  Default division precision 
  private static final int DEF_DIV_SCALE = 10; //  This class cannot be instantiated 

  private Arith() {
  }

  /**
   *  Provides accurate addition operations. 
   * 
   * @param v1
   *       augend 
   * @param v2
   *       addend 
   * @return  The sum of two parameters 
   */
  public static double add(double v1, double v2) {
    BigDecimal b1 = new BigDecimal(Double.toString(v1));
    BigDecimal b2 = new BigDecimal(Double.toString(v2));
    return b1.add(b2).doubleValue();
  }

  /**
   *  Provides accurate subtraction operations. 
   * 
   * @param v1
   *       minuend 
   * @param v2
   *       reduction 
   * @return  The difference between the two parameters 
   */
  public static double sub(double v1, double v2) {
    BigDecimal b1 = new BigDecimal(Double.toString(v1));
    BigDecimal b2 = new BigDecimal(Double.toString(v2));
    return b1.subtract(b2).doubleValue();
  }

  /**
   *  Provides accurate multiplication. 
   * 
   * @param v1
   *       multiplicand 
   * @param v2
   *       The multiplier 
   * @return  The product of two parameters 
   */
  public static double mul(double v1, double v2) {
    BigDecimal b1 = new BigDecimal(Double.toString(v1));
    BigDecimal b2 = new BigDecimal(Double.toString(v2));
    return b1.multiply(b2).doubleValue();
  }

  /**
   *  Provides (relatively) accurate division operation, when the case of endless division, accurate to   After the decimal point 10 Bits, later Numbers 4 Give up 5 Into the. 
   * 
   * @param v1
   *       dividend 
   * @param v2
   *       divisor 
   * @return  The quotient of two parameters 
   */
  public static double div(double v1, double v2) {
    return div(v1, v2, DEF_DIV_SCALE);
  }

  /**
   *  Provides (relatively) accurate division operations. When an inexhaustibility occurs, by scale Parameter refers to   Fixed precision, subsequent Numbers 4 Give up 5 Into the. 
   * 
   * @param v1
   *       dividend 
   * @param v2
   *       divisor 
   * @param scale
   *       To be accurate to a few decimal places. 
   * @return  The quotient of two parameters 
   */
  public static double div(double v1, double v2, int scale) {
    if (scale < 0) {
      throw new IllegalArgumentException(
          "The scale must be a positive integer or zero");
    }
    BigDecimal b1 = new BigDecimal(Double.toString(v1));
    BigDecimal b2 = new BigDecimal(Double.toString(v2));
    return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
  }

  /**
   *  Provide the exact decimal place 4 Give up 5 In the process. 
   * 
   * @param v  Need to be 4 Give up 5 Into the digital 
   * @param scale  Keep a few places behind the decimal point 
   * @return 4 Give up 5 Results after entry 
   */
  public static double round(double v, int scale) {
    if (scale < 0) {
      throw new IllegalArgumentException(
          "The scale must be a positive integer or zero");
    }
    BigDecimal b = new BigDecimal(Double.toString(v));
    BigDecimal one = new BigDecimal("1");
    return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
  }
};


Related articles: